import React, { Component } from 'react'
import './index.css'

export default class Ltem extends Component {
  //存储鼠标状态
  state = {
    mouse: false,
  }

  //事件处理函数
  handerMouse = (value) => {
    return () => {
      this.setState({
        mouse: value
      })
    }
  }
  //取消勾选某个todo
  handleCheck = (id) => {
    return (e) => {
      this.props.changeTodo(id, e.target.checked)
    }
  }
  //点击删除-删除数据
  handleDelete = (id) => {
    return () => {
      if (window.confirm('确定删除吗？')) {
        return this.props.deleteTode(id)
      }
    }
  }

  render() {
    const { id, name, done } = this.props
    return (
      <li onMouseEnter={this.handerMouse(true)} onMouseLeave={this.handerMouse(false)} style={{ backgroundColor: this.state.mouse ? '#ddd' : 'white' }}>
        <label>
          <input type="checkbox" checked={done} onChange={this.handleCheck(id)} />
          <span>{name}</span>
        </label>
        <button className="btn btn-danger" style={{ display: this.state.mouse ? 'block' : 'none' }} onClick={this.handleDelete(id)}>删除</button>
      </li>
    )
  }
}
